package com.admin.filter;

import java.io.IOException;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.admin.bean.AdminUserBean;
import com.admin.bean.VisitLogBean;
import com.admin.service.VisitLogService;
import com.admin.util.CUDUrl;

/**
 * @Title: LoginFilter.java
 * @Package com.admin.filter
 * @Description:
 * @author Jason
 * @date 2010-12-15 下午02:35:15
 * @version
 */

@Repository("loginFilter")
public class LoginFilter implements Filter {

	private static Logger log = Logger.getLogger(LoginFilter.class);
	private static final String[] NO_CHECK_URL = { "/billing/admin/user/phone", "/billing/admin/user/reg", "/billing/admin/order/insert",
			"/billing/admin/adminUser/login","/billing/admin/download/feeFile" };
	@Autowired
	private VisitLogService visitLogService;

	@Override
	public void destroy() {

	}

	@SuppressWarnings("unchecked")
	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) arg0;
		HttpServletResponse response = (HttpServletResponse) arg1;
		String requestURL = request.getRequestURI();// 请求URL

		boolean isCheck = true;
		for (int i = 0; i < NO_CHECK_URL.length; i++) {// 检测请求地址是否需要登录过滤
			String uri = NO_CHECK_URL[i];
			if (uri.equals(requestURL)) {
				log.info("无需登录，直接通过！"+requestURL);
				isCheck = false;
				break;
			}
		}
		if (isCheck) {
			Object obj = request.getSession().getAttribute("adminUser");
			if (obj != null) {
				Map<String, String[]> map = request.getParameterMap();
				insertLog(requestURL, obj, map);
				arg2.doFilter(request, response);
			} else {
				//log.error("未登录，跳转登录页面！");
				response.sendRedirect(request.getContextPath() + "/index.jsp");
			}
		} else {
			arg2.doFilter(arg0, arg1);
		}
	}

	/**
	 * @Title: insertLog
	 * @Description:插入操作Log
	 * @param requestURL
	 * @param obj
	 * @param map
	 * @throws
	 */
	private void insertLog(String requestURL, Object obj, Map<String, String[]> map) {
//		if (CUDUrl.checkLogUrl(requestURL)) {
//			StringBuffer paramters = new StringBuffer(); // 请求参数
//			if (map != null && map.keySet().size() > 0) {
//				for (String key : map.keySet()) {
//					String str[] = map.get(key);
//					for (int i = 0; i < str.length; i++) {
//						paramters.append(key + "=");
//						paramters.append(str[i]);
//						paramters.append("&");
//					}
//				}
//				paramters.deleteCharAt(paramters.lastIndexOf("&"));
//			}
//			VisitLogBean logBean = new VisitLogBean();
//			AdminUserBean user = (AdminUserBean) obj;
//			logBean.setUserId(user.getId());
//			logBean.setUserName(user.getUsername());
//			logBean.setCallUrl(requestURL);
//			logBean.setParamters(paramters.toString().substring(0, paramters.length()>240?240:paramters.length()));
//			visitLogService.addVisitLog(logBean);
//		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {

	}

}
